<template>
  <div class="card-container">
    <!-- 搜索区域 -->

    <!-- 新增删除操作区域 -->
    <div class="create-container">
      <el-button type="primary" @click="dialogVisible=true">添加停车计费规则</el-button>

    </div>

    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="ruleArr">
        <el-table-column type="index" label="序号" />
        <el-table-column label="计费规则编号" prop="ruleNumber" />
        <el-table-column label="计费规则名称" prop="ruleName" />
        <el-table-column label="免费时长（分钟）" prop="freeDuration" />
        <el-table-column label="收费上限（元）" prop="chargeCeiling" />
        <el-table-column label="计费方式" prop="chargeType" :formatter="formatterType" />
        <el-table-column label="计费规则" prop="ruleNameView" />
        <el-table-column label="操作" fixed="right" width="180">
          <template #default="scope">
            <el-button size="mini" type="text" @click="editClick(scope.row)">编辑</el-button>

            <el-button size="mini" type="text" @click="delClick(scope.row.id)">删除</el-button>

          </template>

        </el-table-column>

      </el-table>

    </div>

    <div class="page-container">
      <el-pagination
        layout="total, prev, pager, next"
        :total="total"
        :page-size="ruleList.pageSize"
        @current-change="pageChange"
      />
    </div>

    <!-- 添加楼宇 -->
    <!-- 添加规则 -->
    <!-- 新增弹框 -->
    <el-dialog :visible.sync="dialogVisible" width="680px" title="新增规则">
      <!-- 表单接口 -->
      <div class="form-container">
        <el-form ref="addForm" :model="addForm" :rules="addFormRules" label-position="top">
          <el-form-item label="计费规则编号" prop="ruleNumber">
            <el-input v-model="addForm.ruleNumber" />
          </el-form-item>
          <el-form-item label="计费规则名称" prop="ruleName">
            <el-input v-model="addForm.ruleName" />
          </el-form-item>
          <el-form-item label="计费方式" prop="chargeType">
            <el-radio-group v-model="addForm.chargeType" size="small">
              <el-radio label="duration" border>时长收费</el-radio>
              <el-radio label="turn" border>按次收费</el-radio>
              <el-radio label="partition" border>分段收费</el-radio>
            </el-radio-group>
          </el-form-item>
          <div class="flex-container rules" style="display:flex">
            <el-form-item label="免费时长">
              <el-input
                v-model="addForm.freeDuration"
                controls-position="right"
                :min="1"
                style="width:100px"
              />
              分钟
            </el-form-item>
            <el-form-item label="收费上限">
              <el-input v-model="addForm.chargeCeiling" style="width:120px" />
              元
            </el-form-item>
          </div>
          <!-- == 根据计费方式切换计费规则表单项 == -->
          <el-form-item label="计费规则" class="rules" required>
            <!-- 按时长收费区域 -->
            <div v-if="addForm.chargeType==='duration'" class="duration" style="display:flex">
              每
              <el-form-item prop="durationTime">
                <el-input
                  v-model="addForm.durationTime"
                  controls-position="right"
                  :min="1"
                />
              </el-form-item>
              <el-select v-model="addForm.durationType" placeholder="未选择">
                <el-option
                  v-for="item in ruleNameList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
              <el-form-item prop="durationPrice">
                <el-input v-model="addForm.durationPrice" class="input-box" />
              </el-form-item>
              元
            </div>
            <!-- 按次收费区域 -->
            <div v-if="addForm.chargeType==='turn'" class="turn" style="display:flex">
              每次
              <el-form-item prop="turnPrice">
                <el-input v-model="addForm.turnPrice" class="input-box" />
              </el-form-item>
              元
            </div>
            <!-- 按分段收费区域 -->
            <div v-if="addForm.chargeType==='partition'" class="partition">
              <div class="item" style="display:flex">
                <el-form-item prop="partitionFrameTime">
                  <el-input
                    v-model="addForm.partitionFrameTime"
                    controls-position="right"
                    :min="1"
                  />
                </el-form-item>
                小时内,每小时收费
                <el-form-item prop="partitionFramePrice">
                  <el-input v-model="addForm.partitionFramePrice" class="input-box" style="width:100px" />
                </el-form-item>
                元
              </div>
              <div class="item" style="display:flex;margin:30px auto 0">
                每增加
                <el-form-item prop="partitionIncreaseTime">
                  <el-input
                    v-model="addForm.partitionIncreaseTime"
                    controls-position="right"
                    :min="1"
                  />
                </el-form-item>
                小时,增加
                <el-form-item prop="partitionIncreasePrice">
                  <el-input v-model="addForm.partitionIncreasePrice" class="input-box" style="width:120px;" />
                </el-form-item>
                元
              </div>
            </div>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="addSubmitRules()">确 定</el-button>
      </template>
    </el-dialog>
  </div>

</template>

<script>
import { getRuleAPI, addCarRuleAPI, getRuleDetailAPI, deleteRuleAPI, editRuleAPI } from '@/api/carrule'
export default {
  data() {
    return {
      total: 0,
      pageSize: 5,
      title: '新增规则',
      dialogVisible: false,
      ruleArr: [],
      addForm: {
        ruleNumber: '', // 计费规则编号
        ruleName: '', // 计费规则名称
        chargeType: 'duration', // 计费规则类型 duration / turn / partition
        chargeCeiling: null,
        freeDuration: null,
        // 时长计费独有字段
        durationTime: null, // 时长计费单位时间
        durationPrice: null, // 时长计费单位价格
        durationType: 'hour',
        // 按次收费独有字段
        turnPrice: null,
        // 分段计费独有字段
        partitionFrameTime: null, // 段内时间
        partitionFramePrice: null, // 段内费用
        partitionIncreaseTime: null, // 超出时间
        partitionIncreasePrice: null // 超出费为收费价钱
      },
      ruleNameList: [
        { id: 'minute',
          name: '分钟'

        },
        {
          id: 'hour',
          name: '小时'
        }
      ],
      addFormRules: {
        ruleNumber: [
          {
            required: true,
            message: '请输入规则编号',
            trigger: 'blur'
          }
        ],
        ruleName: [
          {
            required: true,
            message: '请输入规则名称',
            trigger: 'blur'
          }
        ],
        chargeType: [
          {
            required: true,
            message: '请选择收费类型',
            trigger: 'blur'
          }
        ]

      },
      ruleList: {
        page: 1,
        pageSize: 5
      }
    }
  },
  created() {
    this.getRuleList()
  },
  methods: {
    async  getRuleList() {
      const res = await getRuleAPI(this.ruleList)
      console.log(res)
      this.ruleArr = res.data.rows
      this.total = res.data.total
    },
    pageChange(page) {
      this.ruleList.page = page
      this.getRuleList()
    },
    formatterType(row, column, cellValue, index) {
      const obj = {
        'partition': '分段收费',
        'turn': '按次收费',
        'duration': '时长收费'
      }
      return obj[cellValue] || ''
    },
    addSubmitRules() {
      this.$refs.addForm.validate(async valid => {
        if (valid) {
          if (this.title === '新增规则') {
            const res = await addCarRuleAPI(this.addForm)
            if (res.code === 10000) {
              this.$message.success('添加成功')
              this.$refs.addForm.resetFields()
              this.addForm = { chargeType: 'duration' }
              this.getRuleList()
              this.dialogVisible = false
            } else {
              this.$message.error('添加失败' + res.msg)
            }
          } else {
            const res = await editRuleAPI(
              {
                id: this.addForm.id,
                ruleNumber: this.addForm.ruleNumber,
                ruleName: this.addForm.ruleName,
                chargeType: this.addForm.chargeType,
                freeDuration: this.addForm.freeDuration,
                chargeCeiling: this.addForm.chargeCeiling,
                durationTime: this.addForm.durationTime,
                durationPrice: this.addForm.durationPrice,
                turnPrice: this.addForm.turnPrice,
                partitionFrameTime: this.addForm.partitionFrameTime,
                partitionFramePrice: this.addForm.partitionFramePrice,
                partitionIncreaseTime: this.addForm.partitionIncreaseTime,
                partitionIncreasePrice: this.addForm.partitionIncreasePrice
              }
            )
            if (res.code === 10000) {
              this.$message.success('编辑成功')
              this.$refs.addForm.resetFields()
              this.addForm = { chargeType: 'duration' }
              this.dialogVisible = false
              this.getRuleList()
            } else {
              this.$message.error('编辑失败' + res.msg)
            }
          }
        }
      })
    },
    async editClick(row) {
      // console.log(row)
      this.title = '编辑规则'
      this.dialogVisible = true
      const res = await getRuleDetailAPI(row.id)
      this.addForm = res.data
    },
    delClick(id) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await deleteRuleAPI(id)
        if (res.code === 10000) {
          this.$message.success('删除成功')
          this.getRuleList()
        } else {
          this.$message.error('删除失败' + res.msg)
        }
      })
    }

  }
}
</script>

<style lang="scss" scoped>
.card-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;
  .search-main {
    width: 220px;
    margin-right: 10px;
  }
  .search-btn{
    margin-left:20px;
  }
}
.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}
.form-container{
  padding:0px 80px;
}
.flex-container{
  display: flex;
}
.input-box{
  width: 80px;
}
</style>
